package com.ourcraft.informationcollectionsystem.controller.company;

import com.alibaba.fastjson.JSONObject;
import java.util.Map;
import java.util.HashMap;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ourcraft.informationcollectionsystem.entity.User;
import com.ourcraft.informationcollectionsystem.entity.Company;
import com.ourcraft.informationcollectionsystem.mapper.CompanyMapper;
import com.ourcraft.informationcollectionsystem.service.common.CommonService;
import com.ourcraft.informationcollectionsystem.service.common.addQueryHistory.AddHistoryService;
import com.ourcraft.informationcollectionsystem.service.company.companyInfomation.CompanyInformationService;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityCode;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityValidator;
import com.ourcraft.informationcollectionsystem.utils.response.Result;
import com.ourcraft.informationcollectionsystem.utils.response.ResultCode;
import com.ourcraft.informationcollectionsystem.utils.statusCode.HistoryType;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * EnterpriseRecordController handles Enterprise-Record-related operations.
 *
 * @author Xiong Keqin
 * @since 2023-11-02
 */
@RequestMapping("enterprise/enterpriseRecord/")
@RestController
@CrossOrigin
@AuthorityValidator(code = AuthorityCode.RECORD_SUBMIT)
public class EnterpriseRecordController {

    @Autowired
    CompanyInformationService companyInformationService;
    @Autowired
    AddHistoryService addHistoryService;
    @Autowired
    CommonService commonService;

    /**
     * Retrieves Company information by user ID.
     *
     * @param userId The ID of the user.
     * @param request Permission verification related.
     * @return Company information.
     */
    @GetMapping("enterpriseInfo")
    public Result<Map<String, Object>> getCompanyInfo(@RequestParam int userId, HttpServletRequest request) {
        // 创建一个新的Map
        Map<String, Object> data = new HashMap<>();
        Company company = companyInformationService.getCompanyInfo(userId);
        if(company != null){
            data.put("userId", company.getUserId());
            data.put("companyChineseName", company.getCompanyChineseName());
            data.put("companyEnglishName", company.getCompanyEnglishName());
            data.put("companyInstCode", company.getCompanyInstCode());
            data.put("companyPostcode", company.getCompanyPostcode());
            data.put("companyLocation", company.getCompanyLocation());
            data.put("companyIndustry", company.getCompanyIndustry());
            data.put("companyNature", company.getCompanyNature());
            data.put("companyBusiness", company.getCompanyBusiness());
            data.put("companyContacts", company.getCompanyContacts());
            data.put("companyPhone", company.getCompanyPhone());
            data.put("companyAddress", company.getCompanyAddress());
            data.put("companyFax", company.getCompanyFax());
            data.put("companyEmail", company.getCompanyEmail());
            data.put("companyRecordStatus", company.getCompanyRecordStatus());
            data.put("companyId", company.getCompanyId());
        }
        else{
            return Result.fail("未找到企业信息");
        }
        return Result.success(data);
    }

    /**
     * Update company information.
     *
     * @param param The Company information.
     * @param request Permission verification related.
     * @return Operation result.
     */
    @PostMapping("submitEnterpriseInfo")
    public Result<String> submitCompanyInfo(@RequestBody JSONObject param, HttpServletRequest request) {
        // 创建一个新的Map
//        System.out.println(param.toString());
        Map<String, Object> data = new HashMap<>();
        System.out.println("companyNature0:"+param.getString("companyNature"));
        System.out.println("Location0:"+param.getString("companyLocation"));
        Company com1 = new Company(param);
        System.out.println("Location:"+com1.getCompanyLocation());
        Boolean success = companyInformationService.submitCompanyInfo(com1, param.getBoolean("is_Submit"));
        if(success){
            if (param.getBoolean("is_Submit")) {
                System.out.println("\n\n here companyId:" + com1.getCompanyId());
                addHistoryService.addFilingInfoSubmission(
                        commonService.getCompanyId(com1.getUserId()),
                        com1.getUserId(),
                        HistoryType.COMPANY_SUBMIT.ordinal(),
                        "",
                        false
                );
            }
            return Result.success();
        }
        return Result.fail(ResultCode.FAIL,"Failed");
    }

    @GetMapping("testConnection")
    public Result<User> testConnection() {
        return Result.success();
    }
}
